package com.jc.dao.user.impl;

import com.jc.base.Assert;
import com.jc.base.ComponentException;
import com.jc.base.page.Page;
import com.jc.base.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
import com.jc.dao.user.UserStarInfoDao;
import com.jc.domain.user.UserStarInfo;
import com.jc.domain.user.UserStarInfoConditions;
import com.jc.mapper.user.UserStarInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jc.repository.generator.utils.GenerateMybaitsUtils;
import com.jc.repository.generator.utils.Condition;
import com.jc.repository.generator.utils.ConditionsExtend;
import com.jc.utils.CollectionUtils;

import java.util.List;
import java.util.ArrayList;

/**
 * @author
 * @create 2016-12-13 09:32:26
 * @description
 */

@Repository
public class UserStarInfoDaoImpl implements UserStarInfoDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserStarInfoDaoImpl.class);

    @Autowired
    private UserStarInfoMapper mapper;

    //this function automatic generated by application
    @Override
    public UserStarInfo queryUserStarInfoById(Long id) {

        Assert.notNull(id, "用于查询的id不能为空");
        return mapper.selectByPrimaryKey(id);
    }

    //this function automatic generated by application
    @Override
    public ResultData<List<UserStarInfo>> queryUserStarInfosByConditions(UserStarInfo userStarInfo, String orderByClause, Page page) {

        ResultData<List<UserStarInfo>> res = new ResultData<>();
        if(null == userStarInfo && LOGGER.isInfoEnabled()){
            LOGGER.info("用于查询的条件为空, 将查询userStarInfo表的所有数据");
        }
        UserStarInfoConditions conditions = new UserStarInfoConditions();

        UserStarInfoConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionsByReflect(criteria, userStarInfo);

        if (null != page) {
            conditions.setPage(page);
            page.setTotalNum(mapper.countByExample(conditions));
            res.setPage(page);
        }

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        res.setData(mapper.selectByExample(conditions));
        return res;
    }

    //this function automatic generated by application
    @Override
    public List<UserStarInfo> queryUserStarInfosByConditions(UserStarInfo userStarInfo, String orderByClause){

        if(null == userStarInfo && LOGGER.isInfoEnabled()){
        	LOGGER.info("用于查询的条件为空, 将查询userStarInfo表的所有数据");
        }
        UserStarInfoConditions conditions = new UserStarInfoConditions();

        UserStarInfoConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionsByReflect(criteria, userStarInfo);

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        return mapper.selectByExample(conditions);
    }

    //this function automatic generated by application
    @Override
    public List<UserStarInfo> queryUserStarInfosByConditionEx(UserStarInfo userStarInfo, String orderByClause, Condition condition) {

        Assert.notNull(userStarInfo, "用于查询的类,不能为空");
        UserStarInfoConditions conditions = new UserStarInfoConditions();

        UserStarInfoConditions.Criteria criteria = conditions.or();
        GenerateMybaitsUtils.buildConditionEx(criteria,userStarInfo,condition);

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        return mapper.selectByExample(conditions);

    }

    //this function automatic generated by application
    @Override
    public List<UserStarInfo> queryUserStarInfosByConditionExs(ConditionsExtend conditionsExtend, String orderByClause) {
        Assert.notNull(conditionsExtend, "用于查询的类,不能为空");
        UserStarInfoConditions conditions = new UserStarInfoConditions();

        UserStarInfoConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionExs(criteria, conditionsExtend);

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        return mapper.selectByExample(conditions);
    }

    //this function automatic generated by application
    @Override
    public UserStarInfo saveOrInsert(UserStarInfo userStarInfo) {

        Assert.notNull(userStarInfo, "用于保存的对象不能为空");

        GenerateMybaitsUtils.checkNotNull(userStarInfo);

        if (null != userStarInfo.getId()) {
            Assert.notNull(userStarInfo.getId(), "用于更新的对象id不能为空");
            mapper.updateByPrimaryKeySelective(userStarInfo);
        } else {
            mapper.insertSelective(userStarInfo);
        }
        return userStarInfo;

    }

    //this function automatic generated by application
    @Transactional
    @Override
    public List<UserStarInfo> saveOrInsertList(List<UserStarInfo> userStarInfos) {

        Assert.isTrue(CollectionUtils.isNotEmpty(userStarInfos), "添加的userStarInfo集合禁止为空");
        ArrayList<UserStarInfo> userStarInfosSaved = Lists.newArrayListWithCapacity(userStarInfos.size());

        for (UserStarInfo userStarInfo:userStarInfos) {
            userStarInfosSaved.add(saveOrInsert(userStarInfo));
        }
        return userStarInfosSaved;
    }

    //this function automatic generated by application
    @Override
    public boolean deleteUserStarInfoById(Long id) {

        Assert.notNull(id, "属性id不能为空");
        boolean result = false;
        try {
             result =  mapper.deleteByPrimaryKey(id) > 0;
        } catch (Exception e) {
            throw new ComponentException(ComponentException.ErrCode.系统异常,"插入异常");
        }
        return result;
    }

    //this function automatic generated by application
    @Override
    public void deleteByConditions(UserStarInfo userStarInfo) {
        Assert.notNull(userStarInfo, "构造删除条件的类不能为空");

        UserStarInfoConditions conditions = new UserStarInfoConditions();

        UserStarInfoConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionsByReflect(criteria,userStarInfo);
        mapper.deleteByExample(conditions);

    }


}